/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 */

package dao

import (
	"fmt"
	"prim-server/common"
	"prim-server/model"

	"github.com/astaxie/beego/orm"
	"github.com/pkg/errors"
	"github.com/spf13/cast"
)

type OrderUpdateRecordDao struct {
	BaseDao
}

type IOrderUpdateRecordDao interface {
	SaveOrderUpdateRecordInfo(assetUpdateRecord *model.OrderUpdateRecord) error
	DeleteOrderUpdateRecordInfo(userId string) error
	UpdateOrderUpdateRecordInfo(assetUpdateRecord *common.ModifyOrderUpdateRecordDetail) error
	GetOrderUpdateRecordInfo(queryParameter *common.QueryOrderUpdateRecordDetail) (assetUpdateRecord []*model.OrderUpdateRecord, totalCount int, err error)
}

func GetOrderUpdateRecordDao() IOrderUpdateRecordDao {
	dao := &OrderUpdateRecordDao{}
	dao.Init()
	return dao
}

func (ard *OrderUpdateRecordDao) SaveOrderUpdateRecordInfo(assetUpdateRecord *model.OrderUpdateRecord) error {
	if assetUpdateRecord == nil {
		return nil
	}
	_, err := ard.O.Insert(assetUpdateRecord)
	if err != nil {
		_ = ard.O.Rollback()
		return errors.Errorf("SaveOrderUpdateRecordInfo error: %v, userId: %s, indexId: %s", err, assetUpdateRecord.UserId, assetUpdateRecord.IndexId)
	}
	return nil
}

func (ard *OrderUpdateRecordDao) DeleteOrderUpdateRecordInfo(userId string) error {
	if len(userId) == 0 {
		return nil
	}
	assetUpdateRecord := new(model.OrderUpdateRecord)
	assetUpdateRecord.UserId = userId
	_, err := ard.O.Delete(assetUpdateRecord, "user_id")
	if err != nil {
		_ = ard.O.Rollback()
		return errors.Errorf("DeleteOrderUpdateRecordInfo error: %v, userId: %s", err, assetUpdateRecord.UserId)
	}
	return nil
}
func (ard *OrderUpdateRecordDao) UpdateOrderUpdateRecordInfo(assetUpdateRecord *common.ModifyOrderUpdateRecordDetail) error {
	if assetUpdateRecord == nil {
		return nil
	}
	modify := orm.Params{}
	if assetUpdateRecord.OnePayOrderUpdateTime != nil {
		modify["one_pay_update_time"] = ard.ConvertToUTC(*assetUpdateRecord.OnePayOrderUpdateTime)
	}
	if assetUpdateRecord.OnDemandResourceUpdateTime != nil {
		modify["on_demand_update_time"] = ard.ConvertToUTC(*assetUpdateRecord.OnDemandResourceUpdateTime)
	}
	if len(modify) != 0 {
		_, err := ard.O.QueryTable(&model.OrderUpdateRecord{}).Filter("user_id", assetUpdateRecord.UserId).Update(modify)
		if err != nil {
			_ = ard.O.Rollback()
			return errors.Errorf("UpdateOrderUpdateRecordInfo error: %v, userId: %s", err, assetUpdateRecord.UserId)
		}
	}
	return nil
}

func (ard *OrderUpdateRecordDao) GetOrderUpdateRecordInfo(queryParameter *common.QueryOrderUpdateRecordDetail) (assetUpdateRecord []*model.OrderUpdateRecord, totalCount int, err error) {
	if queryParameter == nil {
		return nil, 0, nil
	}
	recode := ard.O.QueryTable(&model.OrderUpdateRecord{})
	finalCondition := orm.NewCondition()
	if len(queryParameter.UserId) > 0 {
		finalCondition = finalCondition.And("user_id__in", queryParameter.UserId)
	}
	offset, pageSize := ard.GetOffsetAndPageSize(nil, nil)
	recode = recode.SetCond(finalCondition)
	totalCount64, err := recode.Count()
	if err != nil {
		return nil, 0, errors.Errorf("GetOrderUpdateRecordInfo error: %v, userId: %s", err, fmt.Sprint(queryParameter.UserId))
	}
	totalCount = cast.ToInt(totalCount64)
	_, err = recode.Offset(offset).Limit(pageSize).All(&assetUpdateRecord)
	if err != nil {
		if err == orm.ErrNoRows {
			//not found
			return nil, 0, nil
		}
		//found error
		return nil, 0, errors.Errorf("GetOrderUpdateRecordInfo error: %v, userId: %s", err, fmt.Sprint(queryParameter.UserId))
	}
	return assetUpdateRecord, totalCount, nil
}
